﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Common;

namespace HanGu.CRM.Areas.CustomerInfo.Controllers
{
    public class MemberChildrenController : AdminBaseController
    {
        public int ParentID { get; set; }
        public List<HanGu.Entity.MemberChildren> sMemberChildren
        {
            get
            {
                if (Session[ParentID + "sMemberChildren"] != null)
                {
                    return Session[ParentID + "sMemberChildren"] as List<HanGu.Entity.MemberChildren>;
                }
                else
                {
                    var _sMemberChildren = new List<HanGu.Entity.MemberChildren>();
                    Session[ParentID + "sMemberChildren"] = _sMemberChildren;
                    return _sMemberChildren;
                }
            }
            set
            {
                foreach (var item in value)
                {
                    sMemberChildren.Remove(sMemberChildren.Where(c => c.ID == item.ID).FirstOrDefault());
                    sMemberChildren.Add(item);
                }
                Session[ParentID + "sMemberChildren"] = sMemberChildren;
            }
        }

        //
        // GET: /CustomerInfo/MemberChildren/
        void GetSexList()
        {
            var lst = serviceDict.GetByGroupName("sex");
            SelectList sList = new SelectList(lst, "GenerateKey", "Value");
            ViewData["SexList"] = sList;
        }
        public ActionResult Index(int id)
        {
            ParentID = id;
            GetSexList();
            var children = serviceMemberInfo.GetAllByParent(id);
            sMemberChildren = children;
            ViewData["ParentID"] = id;
            return View(sMemberChildren);
        }

        //
        // GET: /CustomerInfo/MemberChildren/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /CustomerInfo/MemberChildren/Create
        [HttpGet]
        public ActionResult Create(int? id = null, int? guid = null)
        {
            guid = guid ?? 0;
            ParentID = guid.Value;
            HanGu.Entity.MemberChildren children = new Entity.MemberChildren() { ParentID = guid.Value };
            if (id.HasValue && id.Value > 0)
            {
                children = serviceMemberInfo.GetChildrenByID(id.Value);
            }
            else if (id < 0)
            {
                children = sMemberChildren.Where(c => c.ID == id).FirstOrDefault();
            }
            GetSexList();
            return View(children);
        }

        //
        // POST: /CustomerInfo/MemberChildren/Create
        [HttpPost]
        public string Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                HanGu.Entity.MemberChildren model = new HanGu.Entity.MemberChildren();
                bool result = false;
                TryUpdateModel(model, collection);
                ParentID = model.ParentID;
                if (model.ID > 0)
                {
                    result = serviceMemberInfo.UpdateMemberChildren(model);
                }
                else
                {
                    //result = serviceMemberInfo.AddMemberChildren(model);

                    if (model.ID == 0)
                    {
                        if (model.ParentID == 0)
                        {
                            var minModel = sMemberChildren.Where(c => c.ID <= 0);
                            model.ID = minModel != null && minModel.Count() > 0 ? minModel.Min(c => c.ID) - 1 : -1;
                            sMemberChildren = new List<Entity.MemberChildren>() { model };
                        }
                        else
                        {
                            serviceMemberInfo.AddMemberChildren(model);
                        }
                    }
                    
                    result = true;
                }
                if (result)
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");
                }
                else
                { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", ""); }

            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }



        //
        // GET: /Group/Delete/5
        [HttpPost]
        public string Delete(int id, int? guid = null)
        {
            try
            {
                ParentID = guid != null ? guid.Value : 0;
                if (id < 0)
                {
                    if (sMemberChildren.Remove(sMemberChildren.Where(c => c.ID == id).FirstOrDefault()))
                    {
                        Session[ParentID + "sMemberChildren"] = sMemberChildren;
                        ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
                    }
                    else
                    { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", ""); }

                }
                else
                {
                    if (serviceMemberInfo.DeleteMemberChildren(id))
                    {
                        Session[ParentID + "sMemberChildren"] = null;
                        ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
                    }
                    else
                    { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", ""); }
                }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";

        }
    }
}
